const path = require("path");
const express = require("express");   //引入express
const datas = require("./datas.js");  //引入返回数据
const pathArray = require("./paths.js");  //引入返回数据
const pathData = require("./pathData.js");  //引入返回数据

const app = express(); //实例化express
//解决跨域
app.use((req, res, next) => {
	res.set({
		"Access-Control-Allow-Origin": "*",
		"Access-Control-Allow-Headers": "Origin, Authorization, X-Requested-With, Content-Type, Accept",
		"Access-Control-Allow-Methods": "GET, POST, PATCH, DELETE, OPTIONS",
		"Access-Control-Max-Age": 1728000,//跨域预检查存活时间
		"Content-Security-Policy": "default-src * 'self' localhost:8000 'unsafe-inline' 'unsafe-eval' blob: data: ;",
		"X-Content-Security-Policy": "default-src *",
		"X-WebKit-CSP": "default-src *",
		"Content-Type": "application/json;charset=UTF-8" //配置请求头
	});
	next();
});

app.use("/favicon.ico", express.static(path.join(__dirname, "..", "/public/favicon.ico")));

for (let path of pathArray) {
	if (Object.keys(pathData).indexOf(path) !== -1) {
		app.all("/mall-core" + path, (req, res, next) => {
			console.log(req);
			res.json(pathData[path]);
			next();
		});
	} else {
		app.all("/mall-core" + path, (req, res, next) => {
			console.log(req);
			res.json(datas["blank"]);
			next();
		});
	}
}

app.listen("8001", () => {
	console.log("监听端口 8001");
});
